#include "AffichagePrincipal.hpp"

#include <QtGui/QVBoxLayout>
#include <QtGui/QHBoxLayout>

#include <QtGui/QTabWidget>

#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QTextEdit>
#include <QtGui/QPushButton>
//#include <QtGui/QComboBox>
#include <QtCore/QStringList>

#include <QtCore/QCoreApplication>
#include <QtCore/QSettings>

#include "SimpleCrypt.hpp"

// Temporaire
#include "AfficheAgence.hpp"

// pour debugging uniquement!
//#include "TraitementRequete.hpp"
#include <iostream>

using namespace std;

AffichagePrincipal::AffichagePrincipal( QWidget * parent )
    : QWidget( parent ), TestTemporaire( 0 )
{
    creerUI();
}

AffichagePrincipal::~AffichagePrincipal()
{
    foreach( QLayout * layout, Agencements )
        delete layout;
}

void AffichagePrincipal::chargerReglages()
{
    QCoreApplication::setOrganizationName("JSoloSoft");
    //QCoreApplication::setOrganizationDomain("mysoft.com");
    QCoreApplication::setApplicationName("QCroqueMonster");

    QSettings reglages;

    QString nomEnClair( reglages.value( "NomAgence", QString() ).toString() );
    QString passCrype( reglages.value( "PassAPI", QString() ).toString() );

    SimpleCrypt crypto( Q_UINT64_C( 0xd98691c7fe646343 ) );

    static_cast< QLineEdit* >( Composants.value( "NomAgenceEntree" ) )
            -> setText( nomEnClair );
    static_cast< QLineEdit* >( Composants.value( "ClefApiEntree" ) )
            -> setText( crypto.decryptToString( passCrype ) );
}

void AffichagePrincipal::sauverReglages() const
{
    QCoreApplication::setOrganizationName("JSoloSoft");
    //QCoreApplication::setOrganizationDomain("mysoft.com");
    QCoreApplication::setApplicationName("QCroqueMonster");

    QSettings reglages;

    QString nomEnClair( nomAgence() );
    QString passEnClair( passAPI() );

    SimpleCrypt crypto( Q_UINT64_C( 0xd98691c7fe646343 ) );

    reglages.setValue( "NomAgence", nomEnClair );
    reglages.setValue( "PassAPI",
                       crypto.encryptToString( passEnClair ) );
}

void AffichagePrincipal::recupereTexte( const QString& texte )
{
    //    QTextEdit * tmp =
    //            dynamic_cast< QTextEdit* >( Composants.value( "AffichageCentral" ) );

    //    if ( tmp == 0 )
    //    {
    //        cerr << "Erreur, mauvais type de pointeur" << endl;
    //        cout << texte.toUtf8().constData() << endl;
    //        return;
    //    }

    //    tmp -> setText( texte );

    update();

    emit( majFinie() );
}

void AffichagePrincipal::telechargeFichier( const int& numero )
{
    //    if ( TestTemporaire != 0 )
    //    {
    //        delete TestTemporaire;
    //        TestTemporaire = 0;
    //    }

    //    TestTemporaire = new TraitementRequete( nom, nomAgence(), passAPI(), this );
    QString nomTab( Collection -> tabText( numero ) );
    QWidget * lequel = Collection -> currentWidget();
    if ( nomTab == "agence" )
    {
        AfficheAgence * ptr = dynamic_cast< AfficheAgence* >( lequel );
        if ( lequel == 0 )
        {
            cerr << "Impossible de caster le widget en AfficheAgence" << endl;
            return;
        }
        ptr -> infosActualisees();
    }
}

void AffichagePrincipal::creerUI()
{
    // Ajout de la barre d'info de connection
    QHBoxLayout * BarreInfoLayout = new QHBoxLayout();

    Composants . insert( "NomAgenceLabel",
                         new QLabel( trUtf8( "Nom de l'agence" ), this ) );
    BarreInfoLayout -> addWidget( Composants.value( "NomAgenceLabel" ) );
    Composants . insert( "NomAgenceEntree", new QLineEdit( this ) );
    BarreInfoLayout -> addWidget( Composants.value( "NomAgenceEntree" ) );
    Composants . insert( "ClefApiLabel",
                         new QLabel( trUtf8( "Clé API" ), this ) );
    BarreInfoLayout -> addWidget( Composants.value( "ClefApiLabel" ) );
    Composants . insert(  QString( "ClefApiEntree" ),new QLineEdit( this ) );
    static_cast< QLineEdit* >( Composants.value( "ClefApiEntree" ) )
            -> setEchoMode( QLineEdit::Password );
    BarreInfoLayout -> addWidget( Composants.value( "ClefApiEntree" ) );

    QVBoxLayout * WidgetLayout = new QVBoxLayout();

    Collection = new QTabWidget( this );

    AfficheAgence * affAgence = new AfficheAgence( nomAgence(), passAPI(),
                                                   "agency" );
    WidgetLayout -> addWidget( Collection );
    Composants . insert( "agence", affAgence );

    Collection -> addTab( affAgence, trUtf8( "Agence" ) );

    WidgetLayout -> insertLayout( 0, BarreInfoLayout );

    // Choix
    //    Composants . insert( "Choix", new QComboBox( this ) );
    //    QStringList listeXmls;
    //    listeXmls << "agency" << "syndicate" << "items" << "warlog" << "monsters"
    //              << "portails" << "contracts" << "inventory" << "cities";
    //    static_cast< QComboBox* >( Composants.value( "Choix" ) ) -> addItems( listeXmls );

    // Boutons
    QHBoxLayout * BoutonsLayout = new QHBoxLayout();

//    BoutonsLayout -> addWidget( Composants.value( "Choix" ) );

    Composants . insert( "BoutonConnection",
                         new QPushButton( trUtf8( "&Connection" ), this ) );
    BoutonsLayout -> addWidget( Composants.value( "BoutonConnection" ) );

    WidgetLayout -> insertLayout( 3, BoutonsLayout );

    Agencements . append( BarreInfoLayout );
    Agencements . append( BoutonsLayout );
    Agencements . append( WidgetLayout );

    setLayout( WidgetLayout );

    //chargerReglages();
    connect( Collection, SIGNAL( currentChanged( int ) ),
             this, SLOT( telechargeFichier( int ) ) );

    Collection -> setCurrentIndex( 0 );
    //    connect( static_cast< QComboBox* >( Composants.value( "Choix" ) ),
    //             SIGNAL( currentIndexChanged( QString ) ),
    //             this, SLOT( telechargeFichier( QString ) ) );

    telechargeFichier( 0 );
}

QString AffichagePrincipal::nomAgence() const
{
    return static_cast< QLineEdit* >( Composants.value( "NomAgenceEntree" ) ) -> text();
}

QString AffichagePrincipal::passAPI() const
{
    return static_cast< QLineEdit* >( Composants.value( "ClefApiEntree" ) ) -> text();
}
